package com.crr.common.crud;

import cn.hutool.core.map.MapUtil;
import com.crr.common.bean.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 默认实现
 *
 * @param <T>
 */
public class BaseServiceImpl<T, M extends BaseMapper> implements BaseService<T> {
    @Autowired(required = false)
    protected M baseMapper;

    @Override
    public PageBean query(Map map) {
       /* int page=Integer.parseInt(map.get("page").toString());
        int pageSize=Integer.parseInt(map.get("pageSize").toString());*/
        int page = MapUtil.getInt(map, "page");
        int pageSize = MapUtil.getInt(map, "pageSize");
        PageHelper.startPage(page, pageSize);
        List<Map> list = baseMapper.query(map);
        PageInfo pageInfo = new PageInfo(list);
        return new PageBean(page, pageSize, pageInfo.getTotal(), list);
    }

    @Override
    public int save(T entity) {
        return baseMapper.save(entity);
    }

    @Override
    public int update(T entity) {
        return baseMapper.update(entity);
    }

    @Override
    public int delete(Serializable id) {
        return baseMapper.delete(id);
    }

    @Override
    public T get(Serializable id) {
        return (T) baseMapper.get(id);
    }
}
